package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.TravelCommentReply;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelCommentService travelCommentService;

    /**
     * 1.根据从前台接收的游记id查询分页其下的所有评论
     *
     * @param qo
     * @param id
     * @return
     */
    @GetMapping("/{id}/comments")
    public PageInfo query(TravelCommentQueryObject qo, @PathVariable Long id) {
        qo.setTravelId(id);
        qo.setOrderBy("tc.createTime desc");
        return travelCommentService.query(qo);
    }

    /**
     * 2.保存用户提交的游记评论
     *
     * @param travelComment
     * @return
     */
    @PostMapping("/{travelId}/comments/{authorId}")
    public JSONResult saveComments(@PathVariable Long travelId, TravelComment travelComment) {
        Travel travel = new Travel();
        travelComment.setTravel(travel);
        travelComment.getTravel().setId(travelId);
        JSONResult jsonResult = new JSONResult();
        try {
            travelComment.setUser(UserContext.getCurrentUser());
            travelCommentService.save(travelComment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("保存失败");
        }
        return jsonResult;
    }

    @PostMapping
    public JSONResult save(Travel travel) {
        JSONResult result = new JSONResult();
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.save(travel);
        return result;
    }

    @GetMapping("{id}")
    public Travel getById(@PathVariable Long id) {
        Travel travel = travelService.getById(id);
        return travel;
    }

    @PutMapping(value = "{id}")
    public JSONResult update(Travel travel) {
        travelService.update(travel);
        return new JSONResult();
    }

    @GetMapping
    public PageInfo listAllTravels(TravelObject qo) {
        qo.setOrderBy("t.lastUpdateTime desc");
        qo.setState(Travel.STATE_RELEASE);
        //热门
        qo.setHot(Travel.STATE_HOT);
        //取消热门后的发布状态
        qo.setRelease(Travel.STATE_RELEASE2);
        qo.setIsPublic(true);
        return travelService.query(qo);
    }

    @GetMapping(value = "commends")
    public PageInfo listCommend(TravelObject qo) {
        qo.setOrderBy("tc.schedule desc");
        qo.setPageSize(3);
        return travelCommendService.queryAppList(qo);
    }

    //保存游记评论回复
    @PostMapping("/comments/{commentId}")
    public JSONResult saveReply(TravelCommentReply commentReply){
        commentReply.setUser(UserContext.getCurrentUser());
        travelService.saveReply(commentReply);
        return new JSONResult();
    }
}
